package com.tz.online.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

/**
 * hibernate工具，解析配置文件，并返回一个session对象
 * @author Administrator
 */
public class HibernateUtil {
	private static SessionFactory factory = null;
	//解析配置文件，并且得到一个SessionFactory对象
	static {
		try {
			Configuration cfg = new Configuration().configure();
			ServiceRegistry sr = new StandardServiceRegistryBuilder()
					.applySettings(cfg.getProperties()).build();
			factory = cfg.buildSessionFactory(sr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 返回session
	 */
	public static Session getSession() {
//		return factory == null ? null : factory.openSession();
		return factory == null ? null : factory.getCurrentSession();
	}
	
}
